#include <QtGui>

#include "mainwindow.h"
#include "canvasAppl.h"
#include "aplicationdraw.h"
#include "acmframe.h"

#include <string>

using namespace std;


//
//////////////////////////////////////////////////////////////////////////
MainWindow::MainWindow()
{
  // create canvas to dialog
  m_canvas = new canvasAppl(this);

  setCentralWidget(m_canvas);

  createActions();
  createMenus();
  createContextMenu();
  createToolBars();
  createStatusBar();

  readSettings();

  setWindowIcon(QIcon(":/acmFrame/images/icon.png"));
  setCurrentFile("");

  // fit windows
  if (m_canvas != NULL)
    m_canvas->fit();

  m_frameModel = new acmFrame;
  m_canvas->setModel(m_frameModel);


  setWindowTitle("acmFrame");

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::closeEvent(QCloseEvent *event)
{
  if (okToContinue()) 
  {
    writeSettings();
    event->accept();
  } 
  else 
  {
    event->ignore();
  }
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::newFile()
{
  if (okToContinue()) 
  {
    if (m_frameModel != NULL)
      delete m_frameModel;
    m_frameModel = new acmFrame;
    setCurrentFile("");
  }
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::open()
{
  if (okToContinue()) 
  {
    QString fileName = QFileDialog::getOpenFileName(this,
      tr("Open Geometric Model"), ".",
      tr("Frame files (*.fmg)"));
    if (!fileName.isEmpty())
      loadFile(fileName);
  }
}

//
//////////////////////////////////////////////////////////////////////////
bool MainWindow::save()
{
  if (curFile.isEmpty()) 
  {
    return saveAs();
  }
  else 
  {
    return saveFile(curFile);
  }
}

//
//////////////////////////////////////////////////////////////////////////
bool MainWindow::saveAs()
{
  QString fileName = QFileDialog::getSaveFileName(this,
                                                  tr("Save Geometric Model"), ".",
                                                  tr("Frame files (*.fmg)"));
  if (fileName.isEmpty())
    return false;

  return saveFile(fileName);
}


//
//////////////////////////////////////////////////////////////////////////
void MainWindow::about()
{
    QMessageBox::about(this, tr("About acmFrame"),
            tr("<h2>acmFrame 1.0</h2>"
               "<p>Copyright &copy; 2010 Antonio Miranda."
               "<p>acmFrame is a small application that "
               "allow to draw lines, arcs, circles and splines "
               "to create frame models."));
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::openRecentFile()
{
  if (okToContinue()) 
  {
    QAction *action = qobject_cast<QAction *>(sender());
    if (action)
      loadFile(action->data().toString());
  }
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::updateStatusBar()
{
}


//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createActions()
{
  newAction = new QAction(tr("&New"), this);
  newAction->setIcon(QIcon(":/acmFrame/images/novo.png"));
  newAction->setShortcut(QKeySequence::New);
  newAction->setStatusTip(tr("Create a new Quebra2D file"));
  connect(newAction, SIGNAL(triggered()), this, SLOT(newFile()));

  openAction = new QAction(tr("&Open..."), this);
  openAction->setIcon(QIcon(":/acmFrame/images/abrir.png"));
  openAction->setShortcut(QKeySequence::Open);
  openAction->setStatusTip(tr("Open an existing Quebra2D file"));
  connect(openAction, SIGNAL(triggered()), this, SLOT(open()));

  openNfAction = new QAction(tr("Open Neutral File..."), this);
  // openNfAction->setIcon(QIcon(":/acmFrame/images/open.png"));
  // openNfAction->setShortcut(QKeySequence::Open);
  openNfAction->setStatusTip(tr("Open an existing Neutral file"));
  // connect(openANfction, SIGNAL(triggered()), this, SLOT(openNF()));

  saveAction = new QAction(tr("&Save"), this);
  saveAction->setIcon(QIcon(":/acmFrame/images/salvar.png"));
  saveAction->setShortcut(QKeySequence::Save);
  saveAction->setStatusTip(tr("Save the model to disk"));
  connect(saveAction, SIGNAL(triggered()), this, SLOT(save()));

  saveAsAction = new QAction(tr("Save &As..."), this);
  saveAsAction->setStatusTip(tr("Save the model under a new name"));
  connect(saveAsAction, SIGNAL(triggered()), this, SLOT(saveAs()));

  ImportPriAction = new QAction(tr("&Import Primitives..."), this);
  ImportPriAction->setStatusTip(tr("Import entity primitives to model"));
  // connect(ImportPriAction, SIGNAL(triggered()), this, SLOT(saveAs()));

  for (int i = 0; i < MaxRecentFiles; ++i) 
  {
    recentFileActions[i] = new QAction(this);
    recentFileActions[i]->setVisible(false);
    connect(recentFileActions[i], SIGNAL(triggered()),
      this, SLOT(openRecentFile()));
  }

  exitAction = new QAction(tr("E&xit"), this);
  exitAction->setShortcut(tr("Ctrl+Q"));
  exitAction->setStatusTip(tr("Exit the application"));
  connect(exitAction, SIGNAL(triggered()), this, SLOT(close()));


  //////////////////////////////////////////////////////////////////////////
  // edit actions
  //////////////////////////////////////////////////////////////////////////
  undoAction = new QAction(tr("&Undo"), this);
  undoAction->setIcon(QIcon(":/acmFrame/images/undo.png"));
  undoAction->setShortcut(QKeySequence::Undo);
  undoAction->setStatusTip(tr("Undo last operation"));
  // connect(undoActionnew, SIGNAL(triggered()), this, SLOT(undo()));

  redoAction = new QAction(tr("&Undo"), this);
  redoAction->setIcon(QIcon(":/acmFrame/images/redo.png"));
  redoAction->setShortcut(QKeySequence::Redo);
  redoAction->setStatusTip(tr("Redo last operation"));
  // connect(redoAction, SIGNAL(triggered()), this, SLOT(redo()));

  copyAction = new QAction(tr("&Copy"), this);
  copyAction->setIcon(QIcon(":/acmFrame/images/copia.png"));
  copyAction->setShortcut(QKeySequence::Copy);
  copyAction->setStatusTip(tr("Copy the current selected entity"));
  connect(copyAction, SIGNAL(triggered()), this, SLOT(copyPri()));

  deleteAction = new QAction(tr("&Delete"), this);
  deleteAction->setIcon(QIcon(":/acmFrame/images/eraser.png"));
  deleteAction->setShortcut(QKeySequence::Delete);
  deleteAction->setStatusTip(tr("Delete the current selected entity"));
  connect(deleteAction, SIGNAL(triggered()),  this, SLOT(deletePri()));

  mirrorAction = new QAction(tr("Mirror"), this);
  mirrorAction->setIcon(QIcon(":/acmFrame/images/mirror.png"));
  mirrorAction->setStatusTip(tr("Mirror selected entity"));
  connect(mirrorAction, SIGNAL(triggered()),  this, SLOT(mirrorPri()));

  moveAction = new QAction(tr("Move"), this);
  moveAction->setIcon(QIcon(":/acmFrame/images/move.png"));
  moveAction->setStatusTip(tr("Move the current selected entity"));
  connect(moveAction, SIGNAL(triggered()),  this, SLOT(movePri()));

  rotateAction = new QAction(tr("Rotate"), this);
  rotateAction->setIcon(QIcon(":/acmFrame/images/rotate.png"));
  rotateAction->setStatusTip(tr("Rotate the current selected entity"));
  connect(rotateAction, SIGNAL(triggered()),  this, SLOT(rotatePri()));

  scaleAction = new QAction(tr("Scale"), this);
  scaleAction->setIcon(QIcon(":/acmFrame/images/scala.png"));
  scaleAction->setStatusTip(tr("Scale the current selected entity"));
  connect(scaleAction, SIGNAL(triggered()),  this, SLOT(scalePri()));


  selectAction = new QAction(tr("Set Select Mode"), this);
  // selectAction->setCheckable(true);
  selectAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  selectAction->setStatusTip(tr("Set to select entities"));
  connect(selectAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  keyAction = new QAction(tr("Set Keyboard"), this);
  keyAction->setCheckable(true);
  keyAction->setIcon(QIcon(":/acmFrame/images/teclado.png"));
  keyAction->setStatusTip(tr("Set keyboard on"));
  connect(keyAction, SIGNAL(triggered()),  this, SLOT(setKeyboardOn()));

  //////////////////////////////////////////////////////////////////////////
  // view action
  //////////////////////////////////////////////////////////////////////////
  wireframeAction = new QAction(tr("Wireframe"), this);
  wireframeAction->setCheckable(true);
  wireframeAction->setStatusTip(tr("View wireframe view"));
  // connect(wireframeAction, SIGNAL(triggered()),  this, SLOT(setWireframeMode()));

  isostripAction = new QAction(tr("Contour Isostrip"), this);
  isostripAction->setCheckable(true);
  isostripAction->setStatusTip(tr("Viw contour Isostrip"));
  // connect(isostripAction, SIGNAL(triggered()),  this, SLOT(setContourMode()));

  meshAction = new QAction(tr("Mesh"), this);
  meshAction->setCheckable(true);
  meshAction->setStatusTip(tr("Set View Mesh"));
  // connect(meshAction, SIGNAL(triggered()),  this, SLOT(viewMesh()));

  nodesAction = new QAction(tr("Nodes"), this);
  nodesAction->setCheckable(true);
  nodesAction->setStatusTip(tr("View nodes"));
  // connect(nodesAction, SIGNAL(triggered()),  this, SLOT(viewNodes()));

  nodeNumberAction = new QAction(tr("Node Number"), this);
  nodeNumberAction->setCheckable(true);
  // nodeNumberAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  nodeNumberAction->setStatusTip(tr("View node number"));
  // connect(nodeNumberAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  elemNumberAction = new QAction(tr("Element Number"), this);
  elemNumberAction->setCheckable(true);
  // elemNumberAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  elemNumberAction->setStatusTip(tr("View element number"));
  // connect(elemNumberAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  dualAction = new QAction(tr("Dual Mesh"), this);
  dualAction->setCheckable(true);
  // dualAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  dualAction->setStatusTip(tr("View dual mesh"));
  // connect(selectAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  gaussAction = new QAction(tr("Gauss Points"), this);
  gaussAction->setCheckable(true);
  // gaussAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  gaussAction->setStatusTip(tr("View gauss points"));
  // connect(gaussAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  boundaryAction = new QAction(tr("Boundary"), this);
  boundaryAction->setCheckable(true);
  // boundaryAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  boundaryAction->setStatusTip(tr("View boundary"));
  // connect(selectAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  geomAction = new QAction(tr("Geometric Model"), this);
  geomAction->setCheckable(true);
  // geomAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  geomAction->setStatusTip(tr("View geometric model"));
  // connect(selectAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  atribAction = new QAction(tr("Attributes"), this);
  atribAction->setCheckable(true);
  // atribAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  atribAction->setStatusTip(tr("View attributes"));
  // connect(atribAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  redrawAction = new QAction(tr("Redraw"), this);
  redrawAction->setCheckable(true);
  redrawAction->setShortcut(QKeySequence::Refresh);
  // redrawAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  redrawAction->setStatusTip(tr("Redraw model"));
  // connect(redrawAction, SIGNAL(triggered()),  this, SLOT(setSelectMode()));

  fitAction = new QAction(tr("Fit"), this);
  fitAction->setIcon(QIcon(":/acmFrame/images/zoom_fit.png"));
  fitAction->setStatusTip(tr("Fit model"));
  connect(fitAction, SIGNAL(triggered()),  this, SLOT(zoomFit()));

  zoomAction = new QAction(tr("Zoom Window"), this);
  zoomAction->setIcon(QIcon(":/acmFrame/images/zoom_w.png"));
  zoomAction->setStatusTip(tr("Start zoom window"));
  connect(zoomAction, SIGNAL(triggered()),  this, SLOT(zoomWin()));

  zoomInAction = new QAction(tr("Zoom In"), this);
  zoomInAction->setShortcut(QKeySequence::ZoomIn);
  zoomInAction->setIcon(QIcon(":/acmFrame/images/zoom_in.png"));
  zoomInAction->setStatusTip(tr("Zoom in model"));
  connect(zoomInAction, SIGNAL(triggered()),  this, SLOT(zoomIn()));

  zoomOutAction = new QAction(tr("Zoom Out"), this);
  zoomOutAction->setShortcut(QKeySequence::ZoomOut);
  zoomOutAction->setIcon(QIcon(":/acmFrame/images/zoom_out.png"));
  zoomOutAction->setStatusTip(tr("Zoom out model"));
  connect(zoomOutAction, SIGNAL(triggered()),  this, SLOT(zoomOut()));

  gridAction = new QAction(tr("Grid"), this);
  gridAction->setCheckable(true);
  // gridAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  gridAction->setStatusTip(tr("View Grid"));
  connect(gridAction, SIGNAL(triggered()),  this, SLOT(viewGrid()));

  axisAction = new QAction(tr("Axis"), this);
  axisAction->setCheckable(true);
  // axisAction->setIcon(QIcon(":/acmFrame/images/selecao.png"));
  axisAction->setStatusTip(tr("View Grid"));
  connect(axisAction, SIGNAL(triggered()),  this, SLOT(viewAxis()));


  //////////////////////////////////////////////////////////////////////////
  // draw action
  //////////////////////////////////////////////////////////////////////////
  lineAction = new QAction(tr("&Line"), this);
  lineAction->setShortcut(tr("Ctrl+L"));
  lineAction->setIcon(QIcon(":/acmFrame/images/line.png"));
  lineAction->setStatusTip(tr("Create Line"));
  connect(lineAction, SIGNAL(triggered()),  this, SLOT(createLine()));

  arcAction = new QAction(tr("&Arc"), this);;
  arcAction->setShortcut(tr("Ctrl+A"));
  arcAction->setIcon(QIcon(":/acmFrame/images/arco-circulo.png"));
  arcAction->setStatusTip(tr("Create Arc"));
  connect(arcAction, SIGNAL(triggered()),  this, SLOT(createArc()));

  circleAction = new QAction(tr("Circle"), this);;
  circleAction->setIcon(QIcon(":/acmFrame/images/circulo.png"));
  circleAction->setStatusTip(tr("Create circle"));
  connect(circleAction, SIGNAL(triggered()),  this, SLOT(createCircle()));

  splineAction = new QAction(tr("Spline"), this);;
  splineAction->setIcon(QIcon(":/acmFrame/images/spline.png"));
  splineAction->setStatusTip(tr("Create spline"));
  connect(splineAction, SIGNAL(triggered()),  this, SLOT(createSpline()));

  vertexAction = new QAction(tr("Vertex"), this);;
  vertexAction->setIcon(QIcon(":/acmFrame/images/vertex.png"));
  vertexAction->setStatusTip(tr("Create vertex"));
  connect(vertexAction, SIGNAL(triggered()),  this, SLOT(createVertex()));

  // model build action
  //////////////////////////////////////////////////////////////////////////
  subdvisionAction = new QAction(tr("Curve Subdvision..."), this);
  subdvisionAction->setIcon(QIcon(":/acmFrame/images/subdvision.png"));
  subdvisionAction->setStatusTip(tr("Subdvide current selected curve"));
  // connect(subdvisionAction, SIGNAL(triggered()),  this, SLOT(del()));

  createMeshAction = new QAction(tr("Create Mesh"), this);
  createMeshAction->setIcon(QIcon(":/acmFrame/images/mesh.png"));
  createMeshAction->setStatusTip(tr("Create Mesh on selected face"));
  // connect(createMeshAction, SIGNAL(triggered()),  this, SLOT(del()));

  deleteMeshAction = new QAction(tr("Delete Mesh"), this);
  deleteMeshAction->setStatusTip(tr("Delete Mesh on selected face"));
  // connect(deleteMeshAction, SIGNAL(triggered()),  this, SLOT(del()));

  globalParamAction = new QAction(tr("Global Parameters..."), this);
  // globalParamAction->setIcon(QIcon(":/acmFrame/images/mesh.png"));
  globalParamAction->setStatusTip(tr("Set global parameter of the model"));
  // connect(globalParamAction, SIGNAL(triggered()),  this, ));






  materialAction = new QAction(tr("Material..."), this);
  materialAction->setIcon(QIcon(":/acmFrame/images/material.png"));
  materialAction->setStatusTip(tr("Set Material to frame"));
  connect(materialAction, SIGNAL(triggered()),  this, SLOT(setAttrMaterial()));

  supportAction = new QAction(tr("Support..."), this);
  supportAction->setIcon(QIcon(":/acmFrame/images/restricao.png"));
  supportAction->setStatusTip(tr("Set support restriction"));
  connect(supportAction, SIGNAL(triggered()),  this, SLOT(setAttrRestriction()));


  
  concentLoadAction = new QAction(tr("Concentrated Load..."), this);
  concentLoadAction->setIcon(QIcon(":/acmFrame/images/carga_concentrada.png"));
  concentLoadAction->setStatusTip(tr("Set concentrated load"));
  connect(concentLoadAction, SIGNAL(triggered()),  this, SLOT(setAttrConcLoad()));



  distribLoadAction = new QAction(tr("Distributed Load..."), this);
  distribLoadAction->setIcon(QIcon(":/acmFrame/images/carga_distribuida.png"));
  distribLoadAction->setStatusTip(tr("Create Mesh on selected face"));
  connect(distribLoadAction, SIGNAL(triggered()),  this, SLOT(setAttrDistLoad()));



  sectionAction = new QAction(tr("Section..."), this);
  sectionAction->setIcon(QIcon(":/acmFrame/images/secao.png"));
  sectionAction->setStatusTip(tr("Set Cross Section to frame"));
  connect(sectionAction, SIGNAL(triggered()), this, SLOT(setAttrSection()));

  asCrackAction = new QAction(tr("Set Crack"), this);
  asCrackAction->setIcon(QIcon(":/acmFrame/images/crack.png"));
  asCrackAction->setStatusTip(tr("Set Curve as Crack"));
  connect(asCrackAction, SIGNAL(triggered()),  this, SLOT(setAttrCrack()));

  //////////////////////////////////////////////////////////////////////////
  regularFEMAction = new QAction(tr("Regular Finite Element"), this);
  regularFEMAction->setIcon(QIcon(":/acmFrame/images/regular_fem.png"));
  regularFEMAction->setStatusTip(tr("Perform regular FEM"));
  // connect(createMeshAction, SIGNAL(triggered()),  this, SLOT(del()));

  adaptiveAction = new QAction(tr("Adaptive Regular Finite Element..."), this);
  adaptiveAction->setIcon(QIcon(":/acmFrame/images/adaptive.png"));
  adaptiveAction->setStatusTip(tr("Perform adaptive mesh with FEM"));
  // connect(adaptiveAction, SIGNAL(triggered()),  this, SLOT(del()));

  crackPropAction = new QAction(tr("Crack Propagation..."), this);
  crackPropAction->setIcon(QIcon(":/acmFrame/images/propagation.png"));
  crackPropAction->setStatusTip(tr("Perform crack propagation"));
  // connect(crackPropAction, SIGNAL(triggered()),  this, SLOT(del()));

  aboutAction = new QAction(tr("&About"), this);
  aboutAction->setStatusTip(tr("Show the application's About box"));
  connect(aboutAction, SIGNAL(triggered()), this, SLOT(about()));

  aboutQtAction = new QAction(tr("About &Qt"), this);
  aboutQtAction->setStatusTip(tr("Show the Qt library's About box"));
  connect(aboutQtAction, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createMenus()
{
  fileMenu = menuBar()->addMenu(tr("&File"));
  fileMenu->addAction(newAction);
  fileMenu->addAction(openAction);
  fileMenu->addAction(saveAction);
  fileMenu->addAction(saveAsAction);
  fileMenu->addAction(ImportPriAction);
  separatorAction = fileMenu->addSeparator();
  for (int i = 0; i < MaxRecentFiles; ++i)
    fileMenu->addAction(recentFileActions[i]);
  fileMenu->addSeparator();
  fileMenu->addAction(exitAction);

  //////////////////////////////////////////////////////////////////////////
  editMenu = menuBar()->addMenu(tr("&Edit"));
  editMenu->addAction(undoAction);
  editMenu->addAction(redoAction);
  editMenu->addSeparator ();
  editMenu->addAction(selectAction);
  editMenu->addAction(keyAction);
  editMenu->addSeparator ();
  editMenu->addAction(copyAction);
  editMenu->addAction(deleteAction);
  editMenu->addSeparator ();
  editMenu->addAction(mirrorAction);
  editMenu->addAction(moveAction);
  editMenu->addAction(rotateAction);
  editMenu->addAction(scaleAction);


  //////////////////////////////////////////////////////////////////////////
  viewMenu = menuBar()->addMenu(tr("&View"));
  femSubMenu = viewMenu->addMenu(tr("&FEM itens"));
  femSubMenu->addAction(wireframeAction);
  femSubMenu->addAction(isostripAction);
  femSubMenu->addSeparator ();
  femSubMenu->addAction(meshAction);
  femSubMenu->addAction(nodesAction);
  femSubMenu->addAction(nodeNumberAction);
  femSubMenu->addAction(elemNumberAction);
  femSubMenu->addAction(dualAction);
  femSubMenu->addAction(gaussAction);
  femSubMenu->addAction(boundaryAction);
  //////////////////////////////////////////////////////////////////////////
  viewMenu->addSeparator ();
  viewMenu->addAction(geomAction);
  viewMenu->addAction(atribAction);
  viewMenu->addSeparator ();
  viewMenu->addAction(redrawAction);
  viewMenu->addAction(fitAction);
  viewMenu->addAction(zoomAction);
  viewMenu->addAction(zoomInAction);
  viewMenu->addAction(zoomOutAction);
  viewMenu->addSeparator ();
  viewMenu->addAction(gridAction);
  viewMenu->addAction(axisAction);

  //////////////////////////////////////////////////////////////////////////
  drawMenu = menuBar()->addMenu(tr("&Draw"));
  drawMenu->addAction(lineAction);
  drawMenu->addAction(arcAction);
  drawMenu->addAction(circleAction);
  drawMenu->addAction(splineAction);
  drawMenu->addSeparator ();
  drawMenu->addAction(vertexAction);

  //////////////////////////////////////////////////////////////////////////
  modelMenu = menuBar()->addMenu(tr("&Model"));
  modelMenu->addAction (subdvisionAction);
  modelMenu->addAction (createMeshAction);
  modelMenu->addAction (deleteMeshAction);
  modelMenu->addSeparator ();
  modelMenu->addAction (globalParamAction);
  modelMenu->addAction (materialAction);
  modelMenu->addAction (sectionAction);
  modelMenu->addAction (supportAction);
  modelMenu->addAction (concentLoadAction);
  modelMenu->addAction (distribLoadAction);
  modelMenu->addAction (asCrackAction);


  //////////////////////////////////////////////////////////////////////////
  analysisMenu = menuBar()->addMenu(tr("&Model"));
  analysisMenu->addAction (regularFEMAction);
  analysisMenu->addAction (adaptiveAction);
  analysisMenu->addAction (crackPropAction);


  helpMenu = menuBar()->addMenu(tr("&Help"));
  helpMenu->addAction(aboutAction);
  helpMenu->addAction(aboutQtAction);
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createContextMenu()
{
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createToolBars()
{
  fileToolBar = addToolBar(tr("&File"));
  fileToolBar->addAction(newAction);
  fileToolBar->addAction(openAction);
  fileToolBar->addAction(saveAction);

  drawToolBar = addToolBar(tr("&Draw"));
  addToolBar(Qt::LeftToolBarArea, drawToolBar);
  drawToolBar->addAction (vertexAction);
  drawToolBar->addAction (lineAction);
  drawToolBar->addAction (arcAction);
  drawToolBar->addAction (circleAction);
  drawToolBar->addAction (splineAction);

  editToolBar = addToolBar(tr("&Edit"));
  addToolBar(Qt::LeftToolBarArea, editToolBar);
  editToolBar->addAction(selectAction);
  editToolBar->addAction(keyAction);
  editToolBar->addSeparator ();
  editToolBar->addAction(undoAction);
  editToolBar->addAction(redoAction);
  editToolBar->addAction(copyAction);
  editToolBar->addAction(deleteAction);
  editToolBar->addAction(mirrorAction);
  editToolBar->addAction(moveAction);
  editToolBar->addAction(rotateAction);
  editToolBar->addAction(scaleAction);

  modelToolBar = addToolBar(tr("&Model"));
  modelToolBar->addAction (subdvisionAction);
  modelToolBar->addAction (createMeshAction);
  modelToolBar->addSeparator ();
  // modelToolBar->addAction (globalParamAction);
  modelToolBar->addAction (materialAction);
  modelToolBar->addAction (sectionAction);
  modelToolBar->addAction (supportAction);
  modelToolBar->addAction (concentLoadAction);
  modelToolBar->addAction (distribLoadAction);
  modelToolBar->addAction (asCrackAction);

  viewToolBar = addToolBar(tr("&View"));
  viewToolBar->addAction (fitAction);
  viewToolBar->addAction (zoomAction);
  viewToolBar->addAction (zoomInAction);
  viewToolBar->addAction (zoomOutAction);

  analysisToolBar = addToolBar(tr("Analysis"));
  analysisToolBar->addAction (regularFEMAction);
  analysisToolBar->addAction (adaptiveAction);
  analysisToolBar->addAction (crackPropAction);
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createStatusBar()
{
  positionLabel = new QLabel("X: 00000.00000, Y: 00000.00000");
  positionLabel->setIndent(3);
  // positionLabel->setFixedSize()

  messageLabel = new QLabel;
  messageLabel->setIndent(3);

  if (m_canvas != NULL)
    m_canvas->setMessAndPosLabel(messageLabel, positionLabel);

  statusBar()->addWidget(messageLabel, 1);
  statusBar()->addWidget(positionLabel);

  updateStatusBar();
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::readSettings()
{
  QSettings settings("Software Inc.", "Spreadsheet");

  restoreGeometry(settings.value("geometry").toByteArray());

  recentFiles = settings.value("recentFiles").toStringList();
  updateRecentFileActions();

  //bool showGrid = settings.value("showGrid", true).toBool();
  //showGridAction->setChecked(showGrid);

  //bool autoRecalc = settings.value("autoRecalc", true).toBool();
  //autoRecalcAction->setChecked(autoRecalc);
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::writeSettings()
{
  QSettings settings("Software Inc.", "Spreadsheet");

  settings.setValue("geometry", saveGeometry());
  settings.setValue("recentFiles", recentFiles);
  //settings.setValue("showGrid", showGridAction->isChecked());
  //settings.setValue("autoRecalc", autoRecalcAction->isChecked());
}

//
//////////////////////////////////////////////////////////////////////////
bool MainWindow::okToContinue()
{
  if (m_frameModel != NULL)
  {
    int r = QMessageBox::warning(this, tr("Quebra2D"),
      tr("There is a model in memory.\n"
      "Do you want to save your model?"),
      QMessageBox::Yes | QMessageBox::No
      | QMessageBox::Cancel);
    if (r == QMessageBox::Yes) 
    {
      char   ctmp[1024];
      toChar(curFile, ctmp);
      int ok  = m_frameModel->write(ctmp);
      delete m_frameModel;
      m_frameModel = NULL;
      return ok;
    } 
    else if (r == QMessageBox::Cancel) 
    {
      return false;
    }
  }
  return true;
}

//
//////////////////////////////////////////////////////////////////////////
bool MainWindow::loadFile(const QString &fileName)
{
  char   ctmp[1024];
  toChar(fileName, ctmp);

  if (m_frameModel != NULL)
    delete m_frameModel;
  m_frameModel = new acmFrame;

  if (!m_frameModel->read(ctmp))
  {
    statusBar()->showMessage(tr("Loading canceled"), 2000);
    m_canvas->setModel(NULL);
    return false;
  }

  setCurrentFile(fileName);
  statusBar()->showMessage(tr("File loaded"), 2000);
  m_canvas->setModel(m_frameModel);
  m_canvas->fit();

  return true;
}

//
//////////////////////////////////////////////////////////////////////////
bool MainWindow::saveFile(const QString &fileName)
{
  char   ctmp[1024];
  toChar(fileName, ctmp);

  if (!m_frameModel->write(ctmp)) 
  {
    statusBar()->showMessage(tr("Saving canceled"), 2000);
    return false;
  }

  setCurrentFile(fileName);
  statusBar()->showMessage(tr("File saved"), 2000);
  return true;
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setCurrentFile(const QString &fileName)
{
  curFile = fileName;
  setWindowModified(false);

  QString shownName = tr("Untitled");
  if (!curFile.isEmpty())
  {
    shownName = strippedName(curFile);
    recentFiles.removeAll(curFile);
    recentFiles.prepend(curFile);
    updateRecentFileActions();
  }

  setWindowTitle(tr("%1[*] - %2").arg(shownName).arg(tr("acmFrame")));
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::updateRecentFileActions()
{
  QMutableStringListIterator i(recentFiles);
  while (i.hasNext()) 
  {
    if (!QFile::exists(i.next()))
      i.remove();
  }

  for (int j = 0; j < MaxRecentFiles; ++j) 
  {
    if (j < recentFiles.count()) 
    {
      QString text = tr("&%1 %2")
        .arg(j + 1)
        .arg(strippedName(recentFiles[j]));
      recentFileActions[j]->setText(text);
      recentFileActions[j]->setData(recentFiles[j]);
      recentFileActions[j]->setVisible(true);
    } else 
    {
      recentFileActions[j]->setVisible(false);
    }
  }
  separatorAction->setVisible(!recentFiles.isEmpty());
}

//
//////////////////////////////////////////////////////////////////////////
QString MainWindow::strippedName(const QString &fullFileName)
{
  return QFileInfo(fullFileName).fileName();
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::toChar( const QString qtString, char *new_string )
{
  string tmp = qtString.toStdString ();
  strcpy (new_string, tmp.c_str());
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::zoomIn()
{
  if (m_canvas == NULL)
    return;
  m_canvas->scaleWorld(+0.2);
  m_canvas->refreshPixmap();
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::zoomOut()
{
  if (m_canvas == NULL)
    return;
  m_canvas->scaleWorld(-0.1);
  m_canvas->refreshPixmap();
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::zoomFit()
{
  if (m_canvas == NULL)
    return;
  m_canvas->fit();
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::zoomWin()
{
  m_canvas->zoomWin ( );
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createLine()
{
  if (m_canvas == NULL)
    return;
  m_canvas->createCurve("line");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createArc()
{
  if (m_canvas == NULL)
    return;
  m_canvas->createCurve("arc");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createCircle()
{
  if (m_canvas == NULL)
    return;
  m_canvas->createCurve("circle");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createSpline()
{
  if (m_canvas == NULL)
    return;
  m_canvas->createCurve("spline");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::createVertex()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setSelectMode()
{
  m_canvas->selectEntity ( );
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setKeyboardOn()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::copyPri()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::deletePri()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::mirrorPri()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::movePri()
{
  m_canvas->editEntity("move");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::rotatePri()
{
  m_canvas->editEntity("rotate");
}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::scalePri()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::viewGrid()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::viewAxis()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrSection()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrMaterial()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrRestriction()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrConcLoad()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrDistLoad()
{

}

//
//////////////////////////////////////////////////////////////////////////
void MainWindow::setAttrCrack()
{

}

